﻿var ClassId = 0;
$(document).ready(function () {
    ClassId = parseInt($("#classes a:eq(0)").attr("name"));
    $("#classes a:eq(0)").addClass("current");
    $(".concent").delegate("a", "click",
            function () {
                if ($(this).attr("isleaf") == 'True') {
                    PreviewsList.PageIndex = 1;
                    PreviewsList.GetData(this.id);
                }
            }
        );
});

function SetClassId(cid) {
    
    jQuery("#Class_" + ClassId).removeClass("current");
    ClassId = cid;
    PreviewsList.GetData($('.concent a[class=current]').attr('id'));
    jQuery("#Class_" + ClassId).addClass("current");
}

function PreviewAdd(dom, title, pid, cid, qid) {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=UTF-8",
        url: "Teacher/Previews.aspx/PreviewAdd",
        data: "{Question:'" + decodeURIComponent(title) + "',PreviewId:'" + pid + "',ChapterId:" + cid + ",QuestionId:'" + qid + "',ClassId:" + ClassId + "}",
        dataType: "json",
        success: function (obj) {
            var data = eval('(' + obj.d + ')');
            if (!data.IsSuccess) {
                alert(data.Msg);
                if (data.Msg.indexOf("您已经登录超时") != -1) window.location.href = "/Login.aspx";
            } else {
                var listHtml = "<div class='famliy'>";
                listHtml += "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
                listHtml += "<tr><td valign='top'><table width='100%' border='0' cellspacing='0' cellpadding='0'>";
                listHtml += "<tr><td valign='top'><div class='red'>0</div></td>";
                listHtml += "<td valign='top'><a href='javascript:void(0);' id='" + data.PreviewId + "' name='title' onclick='$(this).parents(\".famliy\").find(\"tr[name=answers]\").slideToggle(\"fast\");'>" + title + "</a></td></tr>";
                listHtml += "</table></td><td align='right'><br />";
                listHtml += "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td align='right' class='green'>";
                listHtml += "<strong><a href='Teacher/PreviewQuickQuery.aspx?p=" + data.PreviewId + "&c=" + cid + "'>展开</a></strong></td><td align='center'><table border='0' cellspacing='0' cellpadding='0'>";
                listHtml += "<tr><td class='green'><a href='javascript:void(0);' onclick='" + "PreviewAdd(this,$(this).parents('.famliy').find('a[name=title]:first').text(),$(this).parents('.famliy').find('a[name=title]:first').attr('id'),$('.concent a[class=current]').attr('id'),$(this).attr('id'));" + "'>引用</a></td>";
                listHtml += "<td><div class='square'></div>";
                listHtml += "</td></tr></table></td></tr></table></td></tr></table></div>";
                $("#nodata").remove();
                $(".title").after(listHtml);
                CheckRef(dom);
            }
        },
        error: function (r, t, e) {
            alert(t);
        }
    });
}

function PerArrayToStr(obj) {
    var str = '';
    $(obj).parents(".famliy").find(".name").each(function () {
        str += $(this).html() + '-';
    });
    return str;
}

function PreviewDel(pid) {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=UTF-8",
        url: "/Teacher/Previews.aspx/PreviewDel",
        data: "{PreviewId:'" + pid + "'}",
        dataType: "json",
        success: function (obj) {
            var data = eval('(' + obj.d + ')');
            if (!data.IsSuccess) {
                alert(data.Msg);
                if (data.Msg.indexOf("您已经登录超时") != -1) window.location.href = "/Login.aspx";
            } else {
                $("#" + pid).parents(".famliy").fadeOut(function () {
                    $("#" + pid).parents(".famliy").remove();
                })

            }
        },
        error: function (r, t, e) {
            alert(t);
        }
    });
}

function UpdateRefCount(obj) {
    var val = parseInt($(obj).parents(".famliy").find("td[name=refcount]:first").text());
    val = isNaN(val) ? 0 : val;
    val += 1;
    $(obj).parents(".famliy").find(":[name=refcount]").text(val)
}

function CheckRef(obj) {
    $(obj).parent().next().find(".square").append('<img src=\"img/success.png\" width=\"15\" height=\"15\" />')
}

var PreviewsList = {
    PageIndex: 1,
    PageSize: 0,
    PageCount: 1,
    ClassId: ClassId,
    GetData: function (ChapterId) {
        $(".famliy").remove();
        $(".title").after("<div class='famliy' style='text-align:center;'><img src='img/load.gif'></img></div>");

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=UTF-8",
            url: "/Teacher/Previews.aspx/GetPriviewList2",
            data: "{ChapterId:" + ChapterId + ",ClassId:" + ClassId + ",iPageIndex:" + PreviewsList.PageIndex + ",iPageSize:" + PreviewsList.PageSize + "}",
            dataType: "json",
            success: function (obj) {
                var data = eval('(' + obj.d + ')');
                PreviewsList.PageCount = data.PageCount == 0 ? 1 : data.PageCount;
                if (!data.IsSuccess) {
                    alert(data.Msg);
                    if (data.Msg.indexOf("您已经登录超时") != -1) window.location.href = "/Login.aspx";
                } else {
                    var table = data.Table.TS_Preview_List;
                    var listHtml = "";
                    if (table.length == 0) {
                        listHtml += "<div class='famliy' id='nodata'>该知识点目前还没有前概念问题!</div>";
                    } else {
                        $(table).each(function (i, n) {
                            var result = n.resultList;
                            listHtml += "<div class='famliy'" + (i % 2 == 0 ? "" : "style = 'background-color: #f2f2f2;'") + ">";
                            listHtml += "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
                            listHtml += "<tr><td valign='top'><table width='100%' border='0' cellspacing='0' cellpadding='0'>";
                            listHtml += "<tr><td valign='top'><div class='red'>" + n.ResultCount + "</div></td>";
                            listHtml += "<td valign='top'><a href='javascript:void(0);' id='" + n.PreviewId + "' name='title' onclick='$(this).parents(\".famliy\").find(\"tr[name=answers]\").slideToggle(\"fast\");'>" + n.Question + "</a></td></tr>";
                            for (var j = 0; j < result.length; j++) {
                                listHtml += "<tr><td valign='top'><div class='name'>" + result[j].ResultPercent + "%</div></td>";
                                listHtml += "<td valign='top' style='color: #0000ff'>" + result[j].Result + "</td></tr>";
                            }
                            listHtml += "</table></td><td align='right'>" + (n.ResultCount > 0 ? ("<img name='chart' src='AnalysisPieMap.aspx?per=" + n.Percents.join('-') + "' width='170' height='170' />") : "") + "<br />";
                            listHtml += "<table width='170px' border='0' cellspacing='0' cellpadding='0'><tr><td align='right' class='green'>";
                            listHtml += "<strong><a href='Teacher/PreviewQuickQuery.aspx?p=" + n.PreviewId + "&c=" + n.ChapterId + "'>展开</a></strong></td><td align='center'><table border='0' cellspacing='0' cellpadding='0'>";
                            listHtml += "<tr><td class='green'><a href='javascript:void(0);' id='" + n.QuestionId + "' onclick=\"" + "PreviewAdd(this,$(this).parents('.famliy').find('a[name=title]:first').text(),$(this).parents('.famliy').find('a[name=title]:first').attr('id'),$('.concent a[class=current]').attr('id'),$(this).attr('id'));\">引用</a></td>";
                            listHtml += "<td><div class='square'></div>";
                            listHtml += "</td></tr></table></td></tr></table></td></tr></table></div>";
                        });
                    }
                    $(".famliy").remove();
                    $(".title").after(listHtml);
                }
            },
            error: function (r, t, e) {
                alert(t);
            }
        });
    },
    BuildPage: function () {
        var prevPageClick = PreviewsList.PageIndex == 1 ? "javascript:void(0);" : "PreviewsList.JumpPage(" + (PreviewsList.PageIndex - 1) + ");";
        var nextPageClick = PreviewsList.PageIndex == PreviewsList.PageCount ? "javascript:void(0);" : "PreviewsList.JumpPage(" + (PreviewsList.PageIndex + 1) + ")";
        var pageHtml = PreviewsList.PageIndex == 1 ? "" : ("<td class='page' style='width: 40px;'><a href='javascript:void(0);' onclick='" + prevPageClick + "'>上一页</a></td>");
        var perPage = 5;
        var startPage = (PreviewsList.PageIndex - Math.floor(perPage / 2)) > 0 ? PreviewsList.PageIndex - Math.floor(perPage / 2) : 1;
        var endPage = (PreviewsList.PageIndex + Math.floor(perPage / 2)) > PreviewsList.PageCount ? PreviewsList.PageCount : PreviewsList.PageIndex + Math.floor(perPage / 2);
        var PageCurrentNumber = PreviewsList.PageIndex;

        if (PreviewsList.PageCount <= 9) {
            for (var i = 1; i <= PreviewsList.PageCount; i++) {
                if (i != PreviewsList.PageIndex) {
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + i + ");\">" + i + "</a></td>";
                } else {
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" class=\"current\">" + i + "</a></td>";
                }
            }
        }
        else {
            if (PreviewsList.PageIndex > 3) {
                if (PreviewsList.PageIndex == 4) {
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + 1 + ");\" class=\"" + (PreviewsList.PageIndex == 1 ? "current" : "") + "\">" + 1 + "</a></td>";
                }
                else if (PreviewsList.PageIndex == 5) {
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + 1 + ");\" class=\"" + (PreviewsList.PageIndex == 1 ? "current" : "") + "\">" + 1 + "</a></td>";
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + 2 + ");\" class=\"" + (PreviewsList.PageIndex == 2 ? "current" : "") + "\">" + 2 + "</a></td>";
                }
                else if (PreviewsList.PageIndex == 6) {
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + 1 + ");\" class=\"" + (PreviewsList.PageIndex == 1 ? "current" : "") + "\">" + 1 + "</a></td>";
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + 2 + ");\" class=\"" + (PreviewsList.PageIndex == 2 ? "current" : "") + "\">" + 2 + "</a></td>";
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + 3 + ");\" class=\"" + (PreviewsList.PageIndex == 3 ? "current" : "") + "\">" + 3 + "</a></td>";
                }
                else {
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + 1 + ");\" class=\"" + (PreviewsList.PageIndex == 1 ? "current" : "") + "\">" + 1 + "</a></td>";
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + 2 + ");\" class=\"" + (PreviewsList.PageIndex == 2 ? "current" : "") + "\">" + 2 + "</a></td>";
                    if (PreviewsList.PageIndex == PreviewsList.PageCount) {
                        pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + (startPage - 2) + ");\" class=\"more\">&#8230;</a></td>";
                    } else {
                        pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + (startPage - 1) + ");\" class=\"more\">&#8230;</a></td>";
                    }

                }
            }
            if (PreviewsList.PageIndex == PreviewsList.PageCount) {
                pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + (startPage - 1) + ");\" class=\"" + (PreviewsList.PageIndex == (startPage - 1) ? "current" : "") + "\">" + (startPage - 1) + "</a></td>";
            }
            for (PageCurrentNumber = startPage; PageCurrentNumber <= endPage; PageCurrentNumber++) {
                pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + PageCurrentNumber + ");\" class=\"" + (PreviewsList.PageIndex == PageCurrentNumber ? "current" : "") + "\">" + PageCurrentNumber + "</a></td>";
            }

            if (PreviewsList.PageIndex == 1 && endPage <= PreviewsList.PageCount) {
                pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + 4 + ");\" class=\"" + (PreviewsList.PageIndex == 4 ? "current" : "") + "\">" + 4 + "</a></td>";
                PageCurrentNumber = PageCurrentNumber + 1;
            }
            if (PreviewsList.PageIndex < PreviewsList.PageCount) {
                if (PageCurrentNumber < PreviewsList.PageCount - 2) {
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + PageCurrentNumber + ");\" class=\"more\">&#8230;</a>";
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + (PreviewsList.PageCount - 1) + ");\" class=\"" + (PreviewsList.PageIndex == (PreviewsList.PageCount - 1) ? "current" : "") + "\">" + (PreviewsList.PageCount - 1) + "</a></td>";
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + PreviewsList.PageCount + ");\" class=\"" + (PreviewsList.PageIndex == PreviewsList.PageCount ? "current" : "") + "\">" + PreviewsList.PageCount + "</a></td>";
                }
                else if (PageCurrentNumber == PreviewsList.PageCount) {
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + PreviewsList.PageCount + ");\" class=\"" + (PreviewsList.PageIndex == PreviewsList.PageCount ? "current" : "") + "\">" + PreviewsList.PageCount + "</a></td>";
                }
                else if (PageCurrentNumber == PreviewsList.PageCount - 1) {
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + (PreviewsList.PageCount - 1) + ");\" class=\"" + (PreviewsList.PageIndex == PreviewsList.PageCount - 1 ? "current" : "") + "\">" + (PreviewsList.PageCount - 1) + "</a></td>";
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + PreviewsList.PageCount + ");\" class=\"" + (PreviewsList.PageIndex == PreviewsList.PageCount ? "current" : "") + "\">" + PreviewsList.PageCount + "</a></td>";
                }
                else if (PageCurrentNumber == PreviewsList.PageCount - 2) {
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + (PreviewsList.PageCount - 2) + ");\" class=\"" + (PreviewsList.PageIndex == (PreviewsList.PageCount - 2) ? "current" : "") + "\">" + (PreviewsList.PageCount - 2) + "</a></td>";
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + (PreviewsList.PageCount - 1) + ");\" class=\"" + (PreviewsList.PageIndex == (PreviewsList.PageCount - 1) ? "current" : "") + "\">" + (PreviewsList.PageCount - 1) + "</a></td>";
                    pageHtml += "<td class='page'><a href=\"javascript:void(0);\" onclick=\"PreviewsList.JumpPage(" + PreviewsList.PageCount + ");\" class=\"" + (PreviewsList.PageIndex == PreviewsList.PageCount ? "current" : "") + "\">" + PreviewsList.PageCount + "</a></td>";
                }
            }
        }
        pageHtml += PreviewsList.PageCount == PreviewsList.PageIndex ? "" : ("<td class='page' style='width: 40px;'><a href='javascript:void(0);' onclick='" + nextPageClick + "'>下一页</a></td>");
        pageHtml += PreviewsList.PageIndex == PreviewsList.PageCount ? "" : ("<td class='page' style='width: 40px;'><a href='javascript:void(0);' onclick='PreviewsList.JumpPage(" + PreviewsList.PageCount + ");'>尾页</a></td>");
        $("#pager").html(pageHtml);
    },
    JumpPage: function (i) {
        var reg = /\d+/;
        if (!reg.test(i)) return;

        if (i < 1) this.PageIndex = 1;
        else if (i > this.PageCount && this.PageCount != -1) this.PageIndex = this.PageCount;
        else this.PageIndex = parseInt(i);
        this.GetData($(".concent a[class='current']").attr("id"));
    }
}
